#include "precompiled.h"
#pragma hdrstop

namespace inf {
    const Plane Plane::ORIGIN( 0.0f, 0.0f, 0.0f, 0.0f );

    /*
    ================
    Plane::type
    ================
    */
    int Plane::type( void ) const {
        if ( normal()[0] == 0.0f ) {
            if ( normal()[1] == 0.0f ) {
                return normal()[2] > 0.0f ? PLANETYPE_Z : PLANETYPE_NEGZ;
            }
            else if ( normal()[2] == 0.0f ) {
                return normal()[1] > 0.0f ? PLANETYPE_Y : PLANETYPE_NEGY;
            }
            else {
                return PLANETYPE_ZEROX;
            }
        }
        else if ( normal()[1] == 0.0f ) {
            if ( normal()[2] == 0.0f ) {
                return normal()[0] > 0.0f ? PLANETYPE_X : PLANETYPE_NEGX;
            }
            else {
                return PLANETYPE_ZEROY;
            }
        }
        else if ( normal()[2] == 0.0f ) {
            return PLANETYPE_ZEROZ;
        }
        else {
            return PLANETYPE_NONAXIAL;
        }
    }

    /*
    ================
    Plane::height_fit
    ================
    */
    bool Plane::height_fit( const Vec3 *points, const int numPoints ) {
        int i;
        float sumXX = 0.0f, sumXY = 0.0f, sumXZ = 0.0f;
        float sumYY = 0.0f, sumYZ = 0.0f;
        Vec3 sum, average, dir;

        if ( numPoints == 1 ) {
            a = 0.0f;
            b = 0.0f;
            c = 1.0f;
            d = -points[0].z;
            return true;
        }
        if ( numPoints == 2 ) {
            dir = points[1] - points[0];
            normal() = dir.cross( Vec3( 0, 0, 1 ) ).cross( dir );
            normalize();
            d = -( normal() * points[0] );
            return true;
        }

        sum.zero();
        for ( i = 0; i < numPoints; i++) {
            sum += points[i];
        }
        average = sum / numPoints;

        for ( i = 0; i < numPoints; i++ ) {
            dir = points[i] - average;
            sumXX += dir.x * dir.x;
            sumXY += dir.x * dir.y;
            sumXZ += dir.x * dir.z;
            sumYY += dir.y * dir.y;
            sumYZ += dir.y * dir.z;
        }

        Mat2 m( sumXX, sumXY, sumXY, sumYY );
        if ( !m.inverse_self() ) {
            a = 0.0f;
            b = 0.0f;
            c = 1.0f;
            d = -points[0].z;
            return false;
        }

        a = - sumXZ * m[0][0] - sumYZ * m[0][1];
        b = - sumXZ * m[1][0] - sumYZ * m[1][1];
        c = 1.0f;
        normalize();
        d = -( a * average.x + b * average.y + c * average.z );
        return true;
    }

    /*
    ================
    Plane::plane_intersection
    ================
    */
    bool Plane::plane_intersection( const Plane &plane, Vec3 &start, Vec3 &dir ) const {
        double n00, n01, n11, det, invDet, f0, f1;

        n00 = normal().length_sqr();
        n01 = normal() * plane.normal();
        n11 = plane.normal().length_sqr();
        det = n00 * n11 - n01 * n01;

        if ( math::fabs(det) < 1e-6f ) {
            return false;
        }

        invDet = 1.0f / det;
        f0 = ( n01 * plane.d - n11 * d ) * invDet;
        f1 = ( n01 * d - n00 * plane.d ) * invDet;

        dir = normal().cross( plane.normal() );
        start = f0 * normal() + f1 * plane.normal();
        return true;
    }

    /*
    =============
    Plane::to_string
    =============
    */
    const char *Plane::to_string( int precision ) const {
        return float_array_to_string_ANSI( to_float_ptr(), get_dimension(), precision );
    }


}   // ::inf
